﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core.Mail;
using Medianamik.Core.Urls;

namespace Medianamik.Core.Reports
{
    public class UrlDictionayEmailReportManager : IReportManager<UrlDictionaryReport>
    {
        private readonly string _sentTo;
        private readonly string _subject;

        public UrlDictionayEmailReportManager(string sentTo, string subject)
        {
            _sentTo = sentTo;
            _subject = subject;
        }

        public void Send(UrlDictionaryReport reportObject)
        {
            var mailReport = new MailManager();

            var message = GetReportBody(reportObject);
            
            mailReport.SendMessage(_subject, message, _sentTo);
        }

        private string GetReportBody(UrlDictionaryReport reportObject)
        {
            return reportObject.ReportedReportAction == Urls.ReportAction.UpdateNodeEntries ?
                BuildUpdateReport(reportObject) :
                BuildLoadReport(reportObject);
        }

        private string BuildUpdateReport(UrlDictionaryReport reportObject)
        {
            var message = new StringBuilder();

            message.AppendLine("Action: UpdateNode");
            message.AppendFormat("Related nodeId: {0}\n", reportObject.UpdatedNodeId);
            message.AppendFormat("Total entries before update: {0}\n", reportObject.NumberTotalOfEntriesBeforeRemove);
            message.AppendFormat("Removed existing entries: {0}\n", reportObject.NumberOfRemovedEntries);
            message.AppendFormat("Added entries: {0}\n", reportObject.NumberOfAddedEntries);
            message.AppendFormat("Number of entries after update: {0}\n", reportObject.NumberTotalOfEntries);
            
            AddSeparetorToMessage(message);
            message.AppendLine("Removed entries:");
            AddEntriesToMessage(reportObject.RemovedEntries, message);

            AddSeparetorToMessage(message);
            message.AppendLine("Added entries:");
            AddEntriesToMessage(reportObject.AddedEntries, message);


            AddSeparetorToMessage(message);
            message.AppendLine("Duplicate entries:");
            AddEntriesToMessage(reportObject.DuplicatedEntries, message);

            return message.ToString();
        }

        private string BuildLoadReport(UrlDictionaryReport reportObject)
        {
            var message = new StringBuilder();

            message.AppendFormat("Action: {0}\n", reportObject.ReportedReportAction == Urls.ReportAction.Initialized ? "Initialize" : "Reload");
            message.AppendFormat("Total entries: {0}\n", reportObject.NumberTotalOfEntries);
            message.AppendFormat("Distinct entries: {0}\n\n", reportObject.NumberOfDistinctEntries);

            AddSeparetorToMessage(message);
            message.AppendLine("Duplicate entries:");
            AddEntriesToMessage(reportObject.DuplicatedEntries, message);

            return message.ToString();
        }

        private void AddSeparetorToMessage(StringBuilder message)
        {
            message.AppendLine("-----------------------------------------------------");
        }

        private void AddEntriesToMessage(IEnumerable<IUrlDictionaryEntry> entries, StringBuilder message)
        {
            entries.ForEach(e => message.AppendFormat("{0} {1}\n", e.NodeId, e.RequestedPath));
        }
    }
}
